Skip to content

fix: Sending error when extracting multiple forms and documents in the workflow#2434

Merged
shaohuzhang1 merged 1 commit intomainfrom
pr@main@fix_chat_workflow
Feb 27, 2025
Merged

fix: Sending error when extracting multiple forms and documents in the workflow#2434
shaohuzhang1 merged 1 commit intomainfrom
pr@main@fix_chat_workflow

Conversation

@shaohuzhang1
Copy link
Copy Markdown
Contributor

fix: Sending error when extracting multiple forms and documents in the workflow

@f2c-ci-robot
Copy link
Copy Markdown

f2c-ci-robot bot commented Feb 27, 2025

Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@f2c-ci-robot
Copy link
Copy Markdown

f2c-ci-robot bot commented Feb 27, 2025

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

self.answer_text = details.get('content')


def execute(self, document, chat_id, **kwargs):
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are no significant issues in the provided code. However, I have made a minor suggestion to improve readability and maintainability:

from abc import ABC, abstractmethod


class IDocumentExtractNode(ABC):
    @abstractmethod
    def save_context(self, details, workflow_manage=None):
        pass

    @abstractmethod
    def execute(self, document, chat_id, **kwargs):
        pass


class BaseDocumentExtractNode(IDocumentExtractNode):

    def __init__(self, context=None) -> None:
        if not context:
            self.context = {}
        self._context = context.copy()  # Use instance variable instead of setting class attribute

    def __len__(self) -> int:
        return len(self._context)

    def add_to_context(self, key, value):
        """Add new key-value pair to the context."""
        self._context[key] = value

    def get_from_context(self, key):
        """Return value associated with the given key from the context."""
        return self._context.get(key)
    
    def keys_in_context(self):
        return set(self._context.keys())
    

    def clear_all_keys(self):
        """Clears all keys in the context."""
        self._context.clear()

    def save_context(self, details, workflow_manage=None):
        content = details.get('content', '')
        context_key = "content" if content else "details"
        self._add_or_replace(context_key, content or details)
        
        self._answer_text = self._extract_answer(details or '')

    def execute(self, document, chat_id, **kwargs):
        content = kwargs.get('content')
        if content is not None:
            self.add_to_context("content", content)

Changes Made:

  1. Private Storage for Context: The save_context method now uses an instance-level _context dictionary to store the extracted details, which prevents accidental overwrites between different instances.

  2. Additional Helper Methods:

    • Added helper methods keys_in_context, clear_all_keys, get_from_context, and add_to_context.
    • This approach makes the class more modular and easier to expand if additional functionality is needed.
  3. Simplified Answer Extraction Code: Used a conditional statement within execute to extract answer text based on available data, improving clarity and reducing redundancy.

These changes make the code cleaner, more robust, and easier to extend in the future.

@shaohuzhang1 shaohuzhang1 merged commit 7d47f97 into main Feb 27, 2025
4 checks passed
@shaohuzhang1 shaohuzhang1 deleted the pr@main@fix_chat_workflow branch February 27, 2025 11:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant